﻿using System;
using System.Text;
using System.Web;


namespace KamLib.Web.MVC
{


    public class MvcElement : IHtmlString
    {
        public string Attributes;
        public string Class;
        public string Style;
        public string Tag;
        public MvcText Text;
        public Action PreRenderer;
        public Action<StringBuilder> Renderer;

        public bool Enabled = true;


        public void ApplyParams(MvcText text)
        {
            if (text == null) return;
            this.Attrs(text.Attrs);
            this.Class(text.Class);
            this.Style(text.Style);
        }

        public void Render(StringBuilder sb)
        {
            if (PreRenderer != null)
                PreRenderer();

            if (Tag.no())
            {
                if (Renderer != null)
                    Renderer(sb);
                else
                    sb.Append(Text);
                return;
            }

            sb.Append('<');
            sb.Append(Tag);

            if (Class.yes())
            {
                sb.Append(@" class=""");
                sb.Append(Class);
                sb.Append(@"""");
            }

            if (Attributes.yes())
            {
                sb.Append(" ");
                sb.Append(Attributes);
            }

            if (Style.yes())
            {
                sb.Append(@" style=""");
                sb.Append(Style);
                sb.Append(@"""");
            }

            sb.Append(">");

            if (Renderer != null)
                Renderer(sb);
            else
                sb.Append(Text);

            sb.Append("</");
            sb.Append(Tag);
            sb.AppendLine(">");
        }


        public string ToHtmlString()
        {
            var sb = new StringBuilder();
            Render(sb);
            return sb.ToString();
        }

        public override string ToString()
        {
            return ToHtmlString();
        }

    }



    public static class MvcElementHelper
    {

        public static T Set<T>(this T me, string tagName, string classes = null, string attrs = null, MvcText text = null)
            where T : MvcElement
        {
            me.Tag = tagName;
            me.Class = classes;
            me.Attributes = attrs;
            me.Text = text;
            return me;
        }

        public static bool yes(this MvcElement me)
        {
            return me != null && me.Text.yes();
        }

        public static bool no(this MvcElement me)
        {
            return me == null || me.Text.no();
        }


        public static T Attrs<T>(this T me, string value)
            where T : MvcElement
        {
            if (me.Attributes.yes())
                me.Attributes += " " + value;
            else
                me.Attributes = value;
            return me;
        }

        public static T Class<T>(this T me, string value)
            where T : MvcElement
        {
            if (me.Class.yes())
                me.Class += " " + value;
            else
                me.Class = value;
            return me;
        }

        public static T Style<T>(this T me, string value)
            where T : MvcElement
        {
            if (value.yes())
                me.Style += value + ";";
            return me;
        }

        public static T Tag<T>(this T me, string value)
            where T : MvcElement
        {
            me.Tag = value;
            return me;
        }

        public static T TagIf<T>(this T me, string value)
            where T : MvcElement
        {
            if (me.Tag.no())
                me.Tag = value;
            return me;
        }

        public static T Text<T>(this T me, MvcText value)
            where T : MvcElement
        {
            me.Text = value;
            return me;
        }


        public static T Enabled<T>(this T me, bool value = true)
            where T : MvcElement
        {
            me.Enabled = value;
            return me;
        }

        public static T Disabled<T>(this T me, bool value = true)
            where T : MvcElement
        {
            me.Enabled = !value;
            return me;
        }


    }



}
